import { createSlice, configureStore } from '@reduxjs/toolkit'
import { Dispatch } from 'react';
const initialState:Store.tokenRes = {
    token: "",
    user: "",
    avatar: ""
}
const tokenSlice = createSlice({
    name: 'token',
    initialState,
    reducers: {
        setToken: (state, actions:{type:string,payload:{token:string,user:string,avatar:string}}) => {
            state.token = actions.payload.token
            state.user = actions.payload.user
            state.avatar = actions.payload.avatar
        },
        removeToken: (state) => {
            state.token = ""
            state.user = ""
            state.avatar = ""
        }
    }
})

export const { setToken,removeToken } = tokenSlice.actions

// 内置了thunk插件，可以直接处理异步请求
export const asyncIncrement = (payload:any) => (dispatch:Dispatch<any>) => {
    setTimeout(() => {
        dispatch(setToken(payload));
    }, 2000);
};

export default tokenSlice.reducer

